package com.zxt.recruit.Controller.Qrcode;

import com.google.code.kaptcha.Producer;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * /api/auth/qrcode
 * 获取四位随机数的验证码
 * @author 24862
 */

@RestController
@RequestMapping("api/auth")
public class CaptchaController {

    @Resource
    Producer producer;

    @RequestMapping("/qrcode/{capText}")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response,
                           @PathVariable("capText") String capText) throws IOException {
        response.setContentType("image/jpeg");
        request.getSession().setAttribute("captcha",capText);
        BufferedImage image = producer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image,"jpg",out);
        out.flush();
    }
}



